package com.tuling.jucdemo.atomic;
/**
 * CAS测试之使用AtomicInteger工具类测试CAS原子性
 * 只能保证一个共享变量原子操作
 */

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicIntegerTest {
    private static AtomicInteger atomicInteger = new AtomicInteger(0);

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(()->{
                for (int j = 0; j < 10000 ; j++) {
                    // 原子自增CAS操作
                    atomicInteger.incrementAndGet();
                    //count++;
                }
            });
            thread.start();
        }

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("atomicInteger:"+atomicInteger.get());
    }
}
